"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import pandas as pd


# from Xinyang_topic.operating_sql import SQLManager


class TopicTag:
    def __init__(self):
        self.all_content = []
        self.topic_Frame = pd.DataFrame(columns=["id", "img_url", "content", "tags"])
        # self.topic_Frame = pd.DataFrame(columns=["序号", "文案", "新标签"])#, "分类"
        self.one_tags = []
        self.two_tags = []
        self.three_tags = []
        self.contents = []
        # self.sql = SQLManager()

    def read_file(self):
        tag_file = pd.read_excel("./标签12.18.xlsx", sheet_name="Sheet1")
        # 读取文件所有标签
        for item in tag_file.values:
            self.one_tags.append(item[0])
        print(len(self.one_tags))

        tag_file = pd.read_excel("./标签12.18.xlsx", sheet_name="Sheet2")
        # 读取文件所有标签
        for item in tag_file.values:
            self.two_tags.append(item[0])
        print(len(self.two_tags))

        tag_file = pd.read_excel("./标签12.18.xlsx", sheet_name="Sheet3")
        # 读取文件所有标签
        for item in tag_file.values:
            self.three_tags.append(item[0])
        print(len(self.three_tags))

    def read_content_file(self):
        content_file = pd.read_excel("./file/weibo.xlsx", sheet_name="weibo")
        # 读取文件所有标签
        for item in content_file.values:
            self.all_content.append(item)
        print(len(self.all_content))

    def get_sql_info(self):
        cx_sql = 'select id,content from collect_moment where id>3648'
        self.contents = self.sql.get_tuple_more(cx_sql)

    def data_parse(self):
        for content in self.contents:
            tag_list = []
            for tag in self.all_tags:
                if tag in content[1]:
                    tag_list.append(tag)
            tags = ",".join(tag_list)
            self.topic_Frame = self.topic_Frame.append(pd.DataFrame({
                "id": [content[0]], "content": [content[1]], "tags": [tags]
            }))
        self.topic_Frame.to_excel("动态内容_sql.xlsx", "动态内容", index=False)

    def data_xlxs_parse(self):
        for content in self.all_content:
            tag_list = []
            for tag in self.one_tags:
                if len(tag_list) > 1:
                    break
                if tag in content[2]:
                    tag_list.append(tag)

            for tag in self.two_tags:
                if len(tag_list) > 0:
                    break
                if tag in content[2]:
                    tag_list.append(tag)

            if len(tag_list) < 3:
                for tag in self.three_tags:
                    if len(tag_list) > 3:
                        break
                    if tag in content[2]:
                        tag_list.append(tag)
            tags = ",".join(tag_list)
            self.topic_Frame = self.topic_Frame.append(pd.DataFrame({
                "id": [content[0]], "img_url": [content[1]], "content": [content[2]], "tags": [tags]
            }))  # ,
        self.topic_Frame.to_excel("new_weibo.xlsx", "Sheet1", index=False)

    def run(self):
        # 读取标签
        self.read_file()

        # 读取sql内容
        # self.get_sql_info()
        # 读取Excel内容
        self.read_content_file()

        # 根据标签分析sql的content，并生成新的xlsx
        # self.data_parse()

        # 根据标签分析Excel的content，并生成新的xlsx
        self.data_xlxs_parse()


if __name__ == '__main__':
    topic = TopicTag()
    topic.run()
